<?php

class Plan extends BaseModel {

    static $table_name = 'planes';
    static $primary_key = 'idplan';
    static $has_many = array(
        array(
            '_ordenes',
            'class_name' => 'Orden',
            'foreign_key' => 'plan'
        ),
        array(
            '_grupos',
            'class_name' => 'GrupoFamiliar',
            'foreign_key' => 'plan'
        ),
        array(
            '_ajustes',
            'class_name' => 'AjusteMedico'
        ),
        array(
            '_profplanes',
            'class_name' => 'ProfPlan',
            'foreign_key' => 'plan'
        ),
        array(
            '_profesionales',
            'class_name' => 'Profesional',
            'through' => '_profplanes'
        )
    );
    static $belongs_to = array(
        array(
            '_empresa',
            'class_name' => 'Empresa',
            'foreign_key' => 'empresa'
        ),
        array(
            '_departamento',
            'class_name' => 'Departamento',
            'foreign_key' => 'departamento'
        )
    );
    
    // Devuelve true si el plan está habilitado para ser usado en la web
    function habilitado(){
        if($this->web_uso == 1): return true;
        else: return false;
        endif;
    }
    
    static function activos_ordered_by_numeroPlan(){
        $planes = Plan::all();
        $numero_planes = array();
        foreach($planes as $plan)
            array_push($numero_planes, $plan->numeroplan);
        $numero_planes = array_unique($numero_planes);
        natsort($numero_planes);
        
        $planes = array();
        foreach($numero_planes as $numero_plan){
            $planes = array_merge($planes, Plan::all(array('numeroPlan' => $numero_plan, 'estado' => 1)));
        }
        
        return $planes;
    }
    
    function to_string(){
        return $this->numeroplan . " - " . $this->siglas . " - " .$this->_departamento->nombre;
    }
}
?>
